package com.client.task;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import com.client.bean.BirdMessage;
import com.client.bean.User;
import com.client.common.ValueStack;
import com.client.db.DataService;
import com.client.net.FormFile;
import com.client.net.Request;
import com.clinet.cache.Log;

/**
 * 提交birdId 服务端clickNum+=1
 * @author Administrator
 *
 */
public class UploadBirdClickNumMessagesTask extends AsyncTask<BirdMessage, Void, Integer> {

	private final Context context;
	private String path = ValueStack.ROOT
			+ "/forestry/birdMessage_updateClickNum.action";
	private DataService mDataService = null;
	private Request mRequest = null;
//http://localhost:8013/forestry/birdMessage_updateClickNum.action?username=admin&password=123456&&birdId=402881e436af954b0136afda9e030019
	public UploadBirdClickNumMessagesTask(Context context, DataService dataService) {
		this.context = context;
		mRequest = new Request(path);
		mDataService=dataService;
//			mDataService = new DataService(context);
	}

	// 正确返回：
	// {"items":[{"birdId":"40289381368be15b01368c0804e20002",
	// "birdImage":"753726d6cdc444cd96ef4d0fd1c18d73.jpg","birdName":"fdasg",
	// "collectLoaction":"","date":"","deadDes":"fdsgfdsg",
	// "deadImage":"381ba853e1dc4114a3b9e19cf3ac7891.jpg","deadNum":0,"direct":"","distance":"","healthNum":0,
	// "kind":"23","latitude":"","longitude":"","num":232,"place":"","sickDes":"43",
	// "sickImage":"81c7484b13664748b5fad4fa60c6b23e.jpg","sickNum":0,"status":"","stayPlace":""}],
	// "message":"OK","success":true,"totalNum":1}
	// 错误返回：
	// {"success":false,"message":"请输入用户名或密码!"}//没用登录的情况

	private List<FormFile> files=new ArrayList<FormFile>(); 
	/**
	 * The task should be executed with one given source photo uri.
	 */
//	private BirdMessage mBirdMessage=null;
	@Override
	protected Integer doInBackground(BirdMessage... params) {
		int code = 0;
		if (params[0] == null) {
			code = 0;
		} else {
			if (!iscancle) {
				User muser = (User) ValueStack.getInstance().getSessionValue(
							"USER");
					Map<String, String> ps = new HashMap<String, String>();
					ps.put("username", muser.userName);
					ps.put("password", muser.password);
					
					ps.put("birdId", params[0].birdId);
					Log.i("birdId "+params[0].birdId);
					String temp="";
						 try {
							temp = mRequest.postFormData(ps);
						} catch (Exception e) {
							Log.i("Exception"+e.getMessage());
						}
//					path = path + ";jsessionid" + muser.sessionid;
//					try {
//						Log.i("postText" + temp);
//						//{"success":true,"id":"40289381368cbe2301368cbf04e00001"}
//						JSONObject obj = new JSONObject(temp);
//						boolean falg = obj.getBoolean("success");
//						if (falg) {
//							code = 1;
//							String id = obj.getString("id");
//							params[0].birdId=id;
//							params[0].uploaded="true";
//							mDataService.updateBirdMessage(params[0]);
//						} else {
//							code = 0;
//						}
//						
//					} catch (Exception e) {
//						Log.i("Exception"+e.getMessage());
//					}
						code = 0;
				} else {
					code = 1;
				}
		}
		return code;
	}
	@Override
	protected void onPostExecute(Integer result) {
//		if (!iscancle && callback != null) {
//			callback.onComplete(result,currPosition);
//		}

	}
	private boolean iscancle = false;
	public void release() {
//		callback = null;
		iscancle=true;
	}
}
